package ru.spbau.database.group3.types;

import java.io.Serializable;
import java.nio.ByteBuffer;

/**
 * @author: stas
 * @date: 2/4/13
 * @time: 1:59 PM
 */
public class IntegerType implements Type, Serializable {
    public IntegerType() {
    }

    @Override
    public Object valueOf(String s) {
        return Integer.valueOf(s);
    }

    @Override
    public int size() {
        return Integer.SIZE / 8;
    }

    @Override
    public void writeValueToByteBuffer(ByteBuffer data, Object keyValue) {
        data.putInt((Integer) keyValue);
    }


    @Override
    public Object readValueFromByteBuffer(ByteBuffer byteBuffer, int position) {
        return byteBuffer.getInt(position);
    }

    @Override
    public int compareTo(Object a, Object b) {
        if(a instanceof Integer && b instanceof Integer) {
            return ((Integer) a).compareTo((Integer) b);
        }
        throw new IllegalArgumentException("Both arguments must have the same type");
    }

	@Override
	public Types getTypeEnum() {
		return Types.INT;
	}
}
